package flc.ast.fragment;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stark.more.MorePrefUtil;
import com.stark.more.about.DefAboutActivity;
import com.stark.usersys.lib.UserModule;
import com.stark.usersys.lib.user.UserApi;
import com.stark.usersys.lib.user.bean.User;
import com.stark.usersysui.lib.login.LoginActivity;
import com.stark.usersysui.lib.vip.VipCenterActivity;

import flc.ast.R;
import flc.ast.activity.SettingActivity;
import flc.ast.databinding.FragmentMyBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.retrofit.INewReqRetCallback;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends BaseNoModelFragment<FragmentMyBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        mDataBinding.ivOpenVip.setOnClickListener(this);
        mDataBinding.llUserCenter.setOnClickListener(this);
        mDataBinding.llSetting.setOnClickListener(this);
        mDataBinding.llAgreement.setOnClickListener(this);
        mDataBinding.llPrivacy.setOnClickListener(this);
        mDataBinding.llFeedback.setOnClickListener(this);
        mDataBinding.llAbout.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivOpenVip) {//开通会员
            if (!UserModule.userManager().isLogin()) {
                LoginActivity.startForRet(this, null);
            } else {
                VipCenterActivity.start(mContext);
            }
        } else if (id == R.id.llSetting) {//设置
            startActivity(SettingActivity.class);
        } else if (id == R.id.llPrivacy) {//隐私政策
            BaseWebviewActivity.openAssetPrivacy(getActivity());
        } else if (id == R.id.llAgreement) {//用户协议
            BaseWebviewActivity.openAssetTerms(mContext);
        } else if (id == R.id.llFeedback) {//意见反馈
            BaseWebviewActivity.openFeedback(getActivity());
        } else if (id == R.id.llAbout) {//关于我们
            startActivity(new Intent(getActivity(), DefAboutActivity.class));
        } else if (id == R.id.llUserCenter) {//用户中心
            UserSysEventProxy.getInstance().goUserCenter(getActivity());
        }
    }

    @Override
    protected void initData() {
        boolean showPersonalRecommend = MorePrefUtil.showPersonalRecommend();
        if (showPersonalRecommend) {
            mDataBinding.llSetting.setVisibility(VISIBLE);
        } else {
            mDataBinding.llSetting.setVisibility(View.GONE);
        }
        mDataBinding.llUserCenter.setVisibility(UserSysEventProxy.getInstance().supportUserSys() ? VISIBLE : View.GONE);
        UserModule.userApi().getUserInfo(null, new INewReqRetCallback<User>() {
            @Override
            public void onResult(int code, String msg, @Nullable User result) {
                if (result == null) {
                    mDataBinding.ivAvatar.setImageResource(R.drawable.d1);
                    mDataBinding.tvName.setText(getString(R.string.please_login));
                    mDataBinding.tvId.setText(getString(R.string.open_vip_tip));
                    mDataBinding.tvId.setTextColor(Color.parseColor("#EC854A"));
                } else {
                    if (result.isVip()) {
                        mDataBinding.ivAvatar.setImageResource(R.drawable.a1);
                        mDataBinding.ivVip.setVisibility(VISIBLE);
                    } else {
                        mDataBinding.ivAvatar.setImageResource(R.drawable.d1);
                        mDataBinding.ivVip.setVisibility(INVISIBLE);
                    }
                    mDataBinding.tvName.setText(result.nickname == null ? getString(R.string.my_text1) : result.nickname);
                    mDataBinding.tvId.setText(getString(R.string.id_text) + result.getId());
                    mDataBinding.tvId.setTextColor(Color.parseColor("#888888"));
                }
            }
        });
    }
}